//
//  JHBookUnitTableViewCell.m
//  JuniRengLish_iOS
//
//  Created by Maiyou on 2019/5/16.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHBookUnitTableViewCell.h"
#import "JHExplainViewController.h"
#import "wslLrcEach.h"

@implementation JHBookUnitTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



#pragma mark - 查看注释
- (IBAction)viewCommentsClick:(id)sender
{
    UIButton * button = sender;
    if (self.isBook)
    {
        NSMutableArray * array = [NSMutableArray array];
        for (wslLrcEach * lrcModel in self.dataArray)
        {
            if ([lrcModel.lrc containsString:@"\n"])
            {
                [array addObject:lrcModel];
            }
        }
        JHExplainViewController * explain = [JHExplainViewController new];
        explain.dataArray = array;
        explain.isBook = self.isBook;
        explain.selectIndex = button.tag;
        [self.currentVC.navigationController pushViewController:explain animated:YES];
    }
    else
    {
        NSString * word = @"";
        if ([self.lrc_en_label.text containsString:@"["])
        {
            NSArray * array = [self.lrc_en_label.text componentsSeparatedByString:@"["];
            word = array[0];
        }
        else
        {
            word = self.lrc_en_label.text;
        }
        [self playWord:word Lan:@"uk"];
        [self getTranferWordInfo:word];
    }
}

- (void)getTranferWordInfo:(NSString *)word
{
    [DJHttpTool postWithURL:@"http://dict.youdao.com/jsonresult" params:@{@"q":word, @"pos":@"yidu", @"type":@"1"} success:^(id json) {
        self.explainView.dataDic = json;
    } failure:^(NSError *error) {
        
    }];
}

- (JHExplainInfoView *)explainView
{
    if (!_explainView)
    {
        _explainView = [[JHExplainInfoView alloc] initWithFrame:CGRectMake(0, 0, JKScreenW, JKScreenH)];
        _explainView.currentVC = self.currentVC;
        [[UIApplication sharedApplication].delegate.window addSubview:_explainView];
    }
    return _explainView;
}

- (FSAudioStream *)audioStream
{
    if (!_audioStream)
    {
        //创建FSAudioStream对象
        _audioStream = [[FSAudioStream alloc]init];
        _audioStream.onFailure = ^(FSAudioStreamError error,NSString *description){
            NSLog(@"播放过程中发生错误，错误信息：%@",description);
        };
        _audioStream.onCompletion = ^(){
            NSLog(@"播放完成!");
        };
        _audioStream.onStateChange = ^(FSAudioStreamState state) {
            
        };
        [_audioStream setVolume:1];//设置声音
    }
    return _audioStream;
}

- (void)playWord:(NSString *)word Lan:(NSString *)lan
{
    word = [word stringByAddingPercentEncodingWithAllowedCharacters:  [NSCharacterSet URLUserAllowedCharacterSet]];
    NSString * urlStr = [NSString stringWithFormat:@"https://fanyi.baidu.com/gettts?lan=%@&text=%@&spd=2&source=web", lan, word];
    self.audioStream.url = [NSURL URLWithString:urlStr];
    [self.audioStream play];
}

@end
